<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模态框封装</title>
    <style>
        .model {
            height: 35px;
            width: 225px;
            display: flex;
            color: #67c23a;
            background: #f0f9eb;
            border-color: #c2e7b0;
            line-height: 35px;
            padding: 0px 20px;
            margin: 20px auto;
            border-radius: 3px;
            border: 1px solid rgb(194, 231, 176);
        }

        .body {
            flex: 1;
        }

        i {
            cursor: pointer;
        }
    </style>
</head>

<body>
    <button id="show">显示</button>

    <script>
        function Model(title = '', message = '') {
            this.modelBox = document.createElement('div')
            this.modelBox.className = 'model'
            this.modelBox.innerHTML = `
                <div class="header">${title}</div>
                <div class="body">${message}</div>
                <i>x</i>
            `
        }

        Model.prototype.open = function () {
            document.body.append(this.modelBox)
            this.modelBox.querySelector('i').addEventListener('click', () => {
                this.close()
            })
        }

        Model.prototype.close = function () {
            this.modelBox.remove()
        }

        document.querySelector('#show').addEventListener('click', () => {
            const show = new Model('提示:', '模态框打开')
            show.open()
        })
    </script>
</body>

</html>